package cc.telecomdigital.tdstock;

import java.util.Vector;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.AsyncTask;
import android.view.View;
import android.view.WindowManager;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.activity.groups.StockGroup;
import cc.telecomdigital.tdstock.activity.groups.stock.CurrentStockInfoActivity;
import cc.telecomdigital.tdstock.view.SunFlowerView;

/**
 * Base AsyncTask execute SunFlower.
 * @author ouyangzhihui
 *
 */
public class BaseAsyncTask extends AsyncTask<String, Void, Void>
{
	private final static String			TAG				= BaseAsyncTask.class
															.getSimpleName();
	protected Context					atContext;
	protected WindowManager				atWindowManager;
	protected  SunFlowerView				sunFlowerView;
	protected static Vector<BaseAsyncTask>	asncTaskManager	= new Vector<BaseAsyncTask>();

	protected static WindowManager.LayoutParams getWMLayoutParams()
	{
		WindowManager.LayoutParams wl = new WindowManager.LayoutParams();

		wl.x = 0;
		wl.y = 0;

		wl.height = WindowManager.LayoutParams.WRAP_CONTENT;
		wl.width = WindowManager.LayoutParams.WRAP_CONTENT;
		wl.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
				| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
				| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
				| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
		wl.format = PixelFormat.TRANSLUCENT;
		wl.windowAnimations = 0;
		return wl;
	}

	public BaseAsyncTask(Context context)
	{
		
		
		TDStockLog.d(TAG, "Context is : " + context.getClass().getSimpleName());
		cancelAllTask();
		asncTaskManager.add(this);
		//		TDStockLog.d(TAG, String.format("the context is %s,child is %s ", context.getClass().getSimpleName(),  ((ActivityGroup)context).getCurrentActivity().getClass().getSimpleName()) );
		if (context instanceof CurrentStockInfoActivity
				|| (context instanceof StockGroup && ((StockGroup) context)
						.getCurrentActivity() instanceof CurrentStockInfoActivity))
		{
			this.atContext = null;
			TDStockLog.d(TAG, "don't use the class in  CurrentStockInfoActivity !");
			return;
		} // StockInfo Page is LightStreamer do not Base SunFlower. use Self.
		this.atContext = context;

	}
	
	public  static  void cancelAllTask() {
		// only one BaseAsyncTask running.
		for (BaseAsyncTask anyTask : asncTaskManager)
		{
			anyTask.cancel(true);
		}
		asncTaskManager.clear();
	}
	
	

	@Override
	protected void onPreExecute()
	{
		TDStockLog.d(TAG, "onPreExecute");
		super.onPreExecute();
		if (atContext == null)
			return;
		startSunFlower(atContext);
	}

	@Override
	protected Void doInBackground(String... params)
	{
		return null;
	}

	@Override
	protected void onPostExecute(Void result)
	{
		TDStockLog.d(TAG, "onPostExecute(result)");
		super.onPostExecute(result);
		if (atContext == null)
			return;
		// clear SunFlower animation.
		clearSunFlower();
	}

	/*
	 * start Sun Flower
	 */
	public void startSunFlower(Context context)
	{
		try
		{
			TDStockLog.d(TAG, "ready to start sunflower");
			// init SunFlower Animation.
			if (atWindowManager == null)
			{
				atWindowManager = (WindowManager) context
						.getSystemService(Context.WINDOW_SERVICE);
			}

			if (sunFlowerView == null)
			{
				sunFlowerView = new SunFlowerView(context);
				sunFlowerView.setVisibility(View.VISIBLE);
				
				atWindowManager.addView(sunFlowerView, getWMLayoutParams());
			}
			else
			{
				TDStockLog
						.d(TAG,
								"don't use the class in  CurrentStockInfoActivity! !");
			}
			
			sunFlowerView.startAnimation();
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	/*
	 * clear SunFlower animation.
	 */
	private void clearSunFlower()
	{
		try
		{
			if (sunFlowerView != null)
			{
				sunFlowerView.setVisibility(View.GONE);
				sunFlowerView.stopAnimation();
				if (atWindowManager != null && sunFlowerView != null)
				{
					atWindowManager.removeView(sunFlowerView);
					sunFlowerView = null;
					TDStockLog.d(TAG, "clearSunFlower successfully!");
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			sunFlowerView = null;
		}
	}

	@Override
	protected void onCancelled()
	{
		TDStockLog.d(TAG, "onCancelled()");
		super.onCancelled();
		// clear SunFlower animation.
		clearSunFlower();
	}
}
